<?php

namespace Apps\Backend\Controllers;

use Phalcon\Mvc\View;
use Apps\Backend\Models\ArticleGame;


class AdminArticleGameController extends \Phalcon\Mvc\Controller
{
    public function initialize()
    {
        $this->tag->setTitle("VEGAFONE");
        $this->tag->setDefaults(array(
            'keywords' => 'VEGAFONE',
            'description' => 'VEGAFONE',
            'backend_home' => 'active'));
        $this->view->users = $ss = $this->session->get('auth-identity');
        if (empty($ss)) {
            $this->response->redirect(PUBLIC_URL_ADMIN);
        }
    }

    public function indexAction()
    {
        $this->view->content = ArticleGame::find();
    }
    public function indexvideoAction()
    {
        $this->view->content = ArticleGame::find("link != '' AND picture = 'no_image.png'");
    }

    public function indexpictureAction()
    {
        $this->view->content = ArticleGame::find("picture != 'no_image.png' AND link = ''");
    }

    public function indexlikeAction()
    {
        $this->view->content = ArticleGame::find(array("orders" => "like DESC"));
    }

    public function editAction($id = null)
    {
        $ss = $this->session->get('auth-identity');
        if ($ss['group_admin'] == 0) {
            $this->response->redirect(PUBLIC_URL_ADMIN . 'login/noAccess');
        }
        $dl = ArticleGame::findFirst("id = '{$id}'");

        $this->view->v = $dl->toArray();
        if ($this->request->isPost()) {

            $data = $this->request->getPost();
            $picture = $this->upload();

            $dl->name = $data['name'];
            $dl->alias = $data['alias'];
            //$dl->keyword=$data['keyword'];
            $dl->link = $data['link'];
            if ($picture != 'no_image.png') {
                $dl->picture = $picture;
            }
            $dl->description = $data['description'];

            $dl->content = $data['content'];
            $dl->status = $data['status'];
            //$dl->type = $data['type'];
            $dl->modified = time();
            $dl->modified_by = $ss['id'];

            $dl->check_out = time();
            $dl->check_timeout = time();
            $dl->save();

            $this->response->redirect(PUBLIC_URL_ADMIN . 'admin-ArticleGame/index');

        }
    }


    public function viewAction($id = null)
    {
        $dl = ArticleGame::findFirst("id = '{$id}'");
        $this->view->v = $dl->toArray();
    }

    public function deleteAction($id = null)
    {

        $dl = ArticleGame::findFirst("id = '{$id}'");
        $dl->delete();
        $this->response->redirect(PUBLIC_URL_ADMIN . 'admin-ArticleGame/index');
    }


    public function addAction()
    {
        $ss = $this->session->get('auth-identity');
        if ($ss['group_admin'] == 0) {
            $this->response->redirect(PUBLIC_URL_ADMIN . 'login/noAccess');
        }
        if ($this->request->isPost()) {
            $data = $this->request->getPost();
            $picture = $this->upload();

            $dl = new ArticleGame();
            $dl->name = $data['name'];
            if (!empty($data['alias'])) {
                $dl->alias = $data['alias'];
            } else {
                $dl->alias = $this->seo->title($data['name']) . time();
            }
            $dl->picture = $picture;
            //$dl->keyword=$data['keyword'];
            $dl->link = $data['link'];
            $dl->description = $data['description'];
            $dl->content = $data['content'];
            $dl->status = $data['status'];
            //$dl->type = $data['type'];
            $dl->view = 1;
            $dl->created = time();
            $dl->created_by = $ss['id'];

            $dl->modified = time();
            $dl->modified_by = $ss['id'];

            $dl->check_out = time();
            $dl->check_timeout = time();
            $dl->save();

            $this->response->redirect(PUBLIC_URL_ADMIN . 'admin-ArticleGame/index');
        }
    }

    private function upload()
    {
        // Check if the user has uploaded files
        if ($this->request->hasFiles() == true) {

            // Print the real file names and sizes
            foreach ($this->request->getUploadedFiles() as $file) {

                //Print file details
                //$file->getName(), " ", $file->getSize(), "\n";
                $type = explode('.', $file->getName());
                $name = time() . rand(9999) . '.' . $type[1];
                //Move the file into the application
                $file->moveTo('files/posts/' . $name);
                return $name;
            }
        } else {
            return 'no_image.png';
        }
    }
}
